/*
 * @Author: your name
 * @Date: 2022-04-26 09:57:55
 * @LastEditTime: 2022-04-26 10:58:13
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \mallAdmin\src\app\main\used-store\used-marketing-manage\components\goods-preview-modal\goods-preview-modal.component.ts
 */
import { Component, OnInit, Input, Output, EventEmitter, forwardRef } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { SecondCouponService } from '../../services/second-coupon.service';

@Component({
  selector: 'goods-preview-modal',
  templateUrl: './goods-preview-modal.component.html',
  styleUrls: ['./goods-preview-modal.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => GoodsPreviewModalComponent),
      multi: true
    }
  ]
})
export class GoodsPreviewModalComponent implements ControlValueAccessor, OnInit {

  @Input() tableDatas!: Array<any>;
  @Input() disable: boolean = false;
  @Input() modalTitle: string = '查看已选';
  @Input() previewIds: Array<any> = [];

  @Output() dataChange = new EventEmitter();

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 10,
    total: 0
  };
  isViewVisible = false;
  _onChange: any = () => {};

  constructor(
    private message: NzMessageService,
    private services: SecondCouponService
  ) { }

  ngOnInit(): void {
  }

  // 查询已选商品
  async queryelectedCouponList() {
    let params  = {
      idList: this.previewIds,
      pageNum: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const res: any = await this.services.getSelectedCouponList(params);
    this.tableParams.loading = false;

    if (res.code === 0) {
      const { records, total } = res.data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(res.message);
    }
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.queryelectedCouponList();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.queryelectedCouponList();
  }

  deleteItem(item: any, index: number) {
    this.tableDatas.splice(index, 1);
    let delteIndex = this.previewIds.findIndex((tag: any) => tag == item.id);
    this.previewIds.splice(delteIndex, 1);
    this.queryelectedCouponList();
    this.dataChange.emit(this.tableDatas);
  }

  handleCancel() {
    this.tableParams.tableData = [];
    this.isViewVisible = false;
    this._onChange(this.isViewVisible);
  }

  writeValue(val: any) {
    if (val) {
      this.queryelectedCouponList();
    }
    this.isViewVisible = val;
  }

  registerOnChange(fn: (_: any) => void): void {
    this._onChange = fn;
  }

  registerOnTouched(fn: any): void {}
}
